<?php 
namespace Admin\Controller;
use Lib\Controller\AdminBaseController;
class PublishController extends AdminBaseController{
    private $PostModel;
    private $PostCountModel;
    private $PostReplyModel;
    private $LikeLogModel;
    private $PostStatusModel;
    
    public function __construct(){
        parent::__construct();
        $this->PostModel = D("Post");
        $this->PostCountModel = D("PostCount");
        $this->PostReplyModel = D("PostReply");
        $this->LikeLogModel = D('LikeLog');
        $this->PostStatusModel = D("PostStatus");
    }
    
    public function add_announcement() {
        if(IS_POST){      
            $data['title'] = I('post.title');
            $data['comment'] = I('post.comment');
            $data['status'] = I('post.status');
            $data['start_time'] = strtotime( I('post.start_time') );
            $data['end_time'] = strtotime( I('post.end_time') );
            $data['create_time'] = time();
            $data['update_time'] = time();
            //$data['member_id'] = session(C('USER_AUTH_KEY'));
            $data['member_id'] = 0;
            //$data['store_id'] = session('shop_id');
            $data['store_id'] = 0;
            $data['topic_type'] = 0;
            
            $id = $this->PostModel->add($data);
            if($id > 0) {
                $pdata['post_id'] = $id;
                $pdata['is_top'] = I('post.is_top');
                $pdata['is_hot'] = I('post.is_hot');
                $pdata['is_essence'] = I('post.is_essence');
                $pdata['is_notice'] = 1;
                $ide = $this->PostStatusModel->add($pdata);
                if($ide > 0){
                    if( $this->PostCountModel->add(array('post_id'=>$id,'like_num'=>0,'reply_num'=>0)) ){
                        $this->tyAjaxReturn(1, '通告帖子成功发布！',U(MODULE_NAME . '/Publish/announcement_list'));
                    }else{
                        $this->PostStatusModel->where(array('id'=>$ide))->delete();
                        $this->tyAjaxReturn(0, '通告帖子发布失败！');
                    }
                    
                }else{
                    $this->PostModel->where(array('id'=>$id))->delete();
                    $this->tyAjaxReturn(0, '通告帖子发布失败！');
                }
            }else{
                $this->tyAjaxReturn(0, '通告帖子发布失败！');
            }           
        }else{            
            //p(session(C('USER_AUTH_KEY')));
            //p(session(shop_id));
            //$res = M('admin')->field('user_name')->where(array('id'=>session(C('USER_AUTH_KEY'))))->find();
            //p($res);
            $this->display();
        }   
    }
    
    public function announcement_list() {
        if(IS_POST){
            $where['p.title'] = array('like','%'.I('post.keywords').'%');
            $where['ps.is_notice'] = 1;
            if(session('shop_id')==0){
                $result = $this->PostStatusModel->alias("as ps")->field('p.id,p.title,p.create_time,p.update_time,p.status')->join(C('DB_PREFIX').'post as p ON ps.post_id = p.id')->where($where)->select();
            }else{
                $where['p.store_id'] = session('shop_id');
                $result = $this->PostStatusModel->alias("as ps")->field('p.id,p.title,p.create_time,p.update_time,p.status')->join(C('DB_PREFIX').'post as p ON ps.post_id = p.id')->where($where)->select();
            }
        }else{      
            if(session('shop_id')==0){
                $result = $this->PostStatusModel->alias("as ps")->field('p.id,p.title,p.create_time,p.update_time,p.status')->join(C('DB_PREFIX').'post as p ON ps.post_id = p.id')->where(array('is_notice' => 1))->select();
            }else{
                $result = $this->PostStatusModel->alias("as ps")->field('p.id,p.title,p.create_time,p.update_time,p.status')->join(C('DB_PREFIX').'post as p ON ps.post_id = p.id')->where(array('is_notice' => 1,'store_id'=>session('shop_id')))->select();
            }
        }
        $this->assign('postInfo',$result);
        $this->display();
    }
       
    public function edit_announcement() {
        if(IS_GET){
            $id = I('get.id', '', 'int');
            $result = $this->PostModel->alias("as p")->field('p.id,p.title,p.comment,p.start_time,p.end_time,p.status,ps.is_top,ps.is_hot,ps.is_essence')->join(C('DB_PREFIX').'post_status as ps ON p.id = ps.post_id ')->where(array('p.id' => $id))->find();
            $result['comment'] = htmlspecialchars_decode($result['comment']);
            $this->assign('postInfo',$result);
            $this->display('add_announcement');
        }
        if(IS_POST){
            $id = I('post.id', '', 'int');
            $data['title'] = I('post.title');
            $data['comment'] = I('post.comment');
            $data['status'] = I('post.status');
            $data['start_time'] = strtotime( I('post.start_time') );
            $data['end_time'] = strtotime( I('post.end_time') );
            $data['update_time'] = time();
            $data['member_id'] = 0;
            $data['store_id'] = 0;
            $data['topic_type'] = 0;
            
            $idc = $this->PostModel->where(array('id' => $id))->save($data);
            
            
            
            $pdata['is_top'] = I('post.is_top');
            $pdata['is_hot'] = I('post.is_hot');
            $pdata['is_essence'] = I('post.is_essence');
            $pdata['is_notice'] = 1;
            $ide = $this->PostStatusModel->where(array('post_id' => $id))->save($pdata);
            //$this->ajaxReturn($this->PostModel->getLastSql());
            if($idc == true || $ide == true) {     
                $this->tyAjaxReturn(1, '通告帖子修改成功！',U(MODULE_NAME . '/Publish/announcement_list'));             
            }else{
                $this->tyAjaxReturn(0, '通告帖子发布失败！',U(MODULE_NAME . '/Publish/announcement_list'));
            }
            
            
        }
        
    }
    

    /**
     * 修改角色状态
     */
    public function change_post_status(){
        $id = I('get.id', '', 'int');
        $where['id'] = $id;
        $data = $this->PostModel->find_post($where);
        //查询之前状态
        //$data = $this->Role->field('status')->find($id);
    
        $status = $data['status'] > 0 ? 0 : 1;
    
        $result = $this->PostModel->where(array('id' => $id))->save(array('status' => $status,'update_time' => time()));
        if ($result > 0) {
            $this->tyAjaxReturn(1, '修改成功！');
        } else {
            $this->tyAjaxReturn(0, '修改失败！');
        }
    }
    
    
    
    public function delete_announcement() {
        $where['id'] = I('get.id');
        $pWhere['post_id'] = I('get.id');
        $postCountResult = $this->PostCountModel->where($pWhere)->delete();
        $postReplyResult = $this->PostReplyModel->where($pWhere)->delete();
        $postStatusResult = $this->PostStatusModel->where($pWhere)->delete();
        $likeLogResult = $this->LikeLogModel->where($pWhere)->delete();
        $postResult = $this->PostModel->where($where)->delete();
        if($postResult && $postCountResult && $postReplyResult && $postStatusResult && $likeLogResult){
            $this->tyAjaxReturn(1,'删除成功',U(MODULE_NAME . '/Publish/announcement_list'));
        }else{
            $this->tyAjaxReturn(1,'删除成功',U(MODULE_NAME . '/Publish/announcement_list'));
            //$this->tyAjaxReturn(0,'删除失败');
        }
    }
    
}
?>